<?
//截取utf8字符串
//$str, $from, $len,$ellipsis


function utf8_substr($conditon)
{

 $result=preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $conditon['from'] . '}' .
'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $conditon['len'] . '}).*#s',
'$1', $conditon['str'] );


    $count = 0;
    for ($i = 0; $i < strlen($conditon['str']); $i++) {
        $value = ord($conditon['str'][$i]);
        if ($value > 127) {
            $count++;
            if ($value >= 192 && $value <= 223) $i++;
            elseif ($value >= 224 && $value <= 239) $i = $i + 2;
            elseif ($value >= 240 && $value <= 247) $i = $i + 3;
            else die('Not a UTF-8 compatible string');
        }
        $count++;
    }

    $new_long=$count / 2;


if( $new_long > $conditon['len'] )
{
    $result =  $result.$conditon['ellipsis'];
}



return $result;
}
?>
